
package auctionhaus


import java.sql.DatabaseMetaData;


import grails.test.mixin.*
import groovy.mock.interceptor.MockFor;

import org.junit.*

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(CrudService)
@Mock(Customer)
class CrudServiceTests {
	
	/**
	 * SRV-4: Refactor your Controllers from the previous assignments to use the service methods created in this section (unit test)
	 */

	def crudService = new CrudService();
	Customer customer;

	@Before
	void beforeTest(){
		def springSecurityService = new Object()
		customer = new Customer()
		springSecurityService.metaClass.encodePassword = {String password -> "ENCODED_PASSWORD"}
		customer.springSecurityService = springSecurityService
	}

	void testCreateCustomerValid(){
		customer.username = "bill@espn.com"
		customer.password = "thispass"
		assertTrue(crudService.createCustomer(customer))
	}

	void testCreateCustomerInvalid(){
		customer.username = "billespn.com"
		customer.password = "thispass"
		assertFalse(crudService.createCustomer(customer))
	}

	void testCreateListingValid(){
		mockDomain(Listing)
		customer.username = "bill@espn.com"
		customer.password = "thispass"
		assertTrue(crudService.createCustomer(customer))

		Listing listing = new Listing();
		listing.name = "Test"
		listing.endDateTime = new Date()+1
		listing.seller = customer
		listing.startingPrice = 1.01
		assertTrue(crudService.createListing(listing))
	}

	void testCreateListingInvalid(){

		mockDomain(Listing)

		customer.username = "bill@espn.com"
		customer.password = "thispass"
		assertTrue(crudService.createCustomer(customer))

		Listing listing = new Listing();
		listing.name = "Test"
		listing.endDateTime = new Date()+1
		assertFalse(crudService.createListing(listing))

	}

	void testCreateBidValid(){
		mockDomain(Bid)
		mockDomain(Listing)
		customer.username = "bill@espn.com"
		customer.password = "thispass"
		assertTrue(crudService.createCustomer(customer))

		Listing listing = new Listing();
		listing.name = "Test"
		listing.endDateTime = new Date()+1
		listing.seller = customer
		listing.startingPrice = 1.01
		assertTrue(crudService.createListing(listing))
		
		Bid bid = new Bid()
		bid.bidder=customer
		bid.amount = 2.00
		bid.listing = listing
		
		assertTrue(crudService.createBid(bid))
	}
	
	void testCreateBidInvalid(){
		mockDomain(Bid)
		mockDomain(Listing)
		customer.username = "bill@espn.com"
		customer.password = "thispass"
		assertTrue(crudService.createCustomer(customer))

		Listing listing = new Listing();
		listing.name = "Test"
		listing.endDateTime = new Date()+1
		listing.seller = customer
		listing.startingPrice = 1.01
		assertTrue(crudService.createListing(listing))
		
		Bid bid = new Bid()
		bid.bidder=customer
		bid.amount = 1.00
		bid.listing = listing
		
		assertFalse(crudService.createBid(bid))
	}
	
	//C-3: When a listing completes it can no longer accept any more bids.  Verify this by testing through the service method created for SRV-3 (unit test).
	void testCreateBidOnCompletedListing(){
		mockDomain(Bid)
		mockDomain(Listing)
		customer.username = "bill@espn.com"
		customer.password = "thispass"
		assertTrue(crudService.createCustomer(customer))

		Listing listing = new Listing();
		listing.name = "Test"
		listing.endDateTime = new Date()+1
		listing.seller = customer
		listing.startingPrice = 1.01
		assertTrue(crudService.createListing(listing))
		
		Bid bid = new Bid()
		bid.bidder=customer
		bid.amount = 12.00
		listing.endDateTime = new Date()-1
		bid.listing = listing
		
		assertFalse(crudService.createBid(bid))
	}
}
